//
//  CMDBVertifyPwdViewController.swift
//  CMDB
//
//  Created by 王磊 on 22/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

class CMDBVertifyPwdViewController: NormalTitleViewController {
    
    internal enum CMDBVerifyType: Int {
        
        case Email
        
        case Phone
        
        case Unknown
    }
    
    internal var verifyType: CMDBVertifyPwdViewController.CMDBVerifyType = .Unknown
    
    private var originalPwd : CMDBSubRegisterTF = CMDBSubRegisterTF()
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        originalPwd.becomeFirstResponder()
    }
    
    override func setup() {
        super.setup()
        
        buildSubview()
    }
    private var nextBtn: UIButton = UIButton(type: .Custom)
    
    private func buildSubview() {
        
        contentView!.backgroundColor = UIColor.whiteColor()
        
        contentView!.addSubview(originalPwd)
        
        contentView!.addSubview(nextBtn)
        
        let textAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: word87Color]
        
        let placeHolderAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: CMDB_Register_TF_PlaceHolder_Color]
        
        originalPwd.attrText = NSMutableAttributedString(string: "登录密码:", attributes: textAttr)
        
        originalPwd.placeHolderAttr = NSAttributedString(string: "请验证您的登录密码", attributes: placeHolderAttr)
        
        originalPwd.keyboardType = .Default
        
        originalPwd.returnKeyType = .Done
        
        originalPwd.maxLength = 11
        
        originalPwd.addTarget(self, action: #selector(returnKey), controlEvent: .ReturnKey)
        
        let Left_Margin: CGFloat = (CMDB_Screen_Width - 630 * CMDB_Rate) * 0.5
        
        originalPwd.frame = CGRect(x: Left_Margin, y: 20 , width:630 * CMDB_Rate , height: 60)
        
        nextBtn.setTitle("下一步", forState: .Normal)
        
        nextBtn.setTitle("下一步", forState: .Highlighted)
        
        nextBtn.setTitleColor(UIColor.Hex6(0xffffff, alpha: 0.54), forState: .Highlighted)
        
        nextBtn.setTitleColor(UIColor.Hex6(0xffffff), forState: .Normal)
        
        nextBtn.backgroundColor = UIColor.Hex6(0xff6d00, alpha: 54)
        
        nextBtn.titleLabel!.font = CMDB_17_FONT
        
        nextBtn.addTarget(self, action: #selector(nextBtnTriggrred), forControlEvents: .TouchUpInside)
        
        nextBtn.layer.cornerRadius = 5
        
        nextBtn.frame = CGRectMake(Left_Margin, originalPwd.y + 60 + 40, originalPwd.width, 88 * CMDB_Rate)
    }
}

extension CMDBVertifyPwdViewController {
    
    @objc private func returnKey() {
        
        view.endEditing(true)
        
        nextBtnTriggrred()
        
    }
    
    @objc private func nextBtnTriggrred() {
        
        if verifyType == .Email {
            
            pushBindEmail()
            
        } else if verifyType == .Phone {
            
            pushBindPhone()
        }
        
        
        //        let verifyPwdParam = CMDBVerifyPasswordParam()
        //
        //        verifyPwdParam.password = originalPwd.text ?? ""
        //
        //        do {
        //
        //            try CMDBUserInfoManager.getInstance.vertifyPassword(verifyPwdParam, handler: { [weak self] (verifyPwdBean) in
        //
        //                if self!.verifyType == .Email {
        //
        //                    self!.pushBindEmail()
        //                } else if self!.verifyType == .Phone {
        //
        //                    self!.pushBindEmail()
        //                }
        //
        //                }, error: { (rescode) in
        //
        //                }, failure: {
        //
        //
        //            })
        //
        //        } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PasswordInvalid) {
        //
        //            CMDBShowInfoManager.getInstance.showInfoWithStatus("密码长度不够")
        //
        //        } catch {
        //
        //            printLog(error)
        //
        //        }
        
    }
    @objc private func pushBindPhone() {
        
        let modifyPhone = CMDBModifyPhoneViewController()
        
        modifyPhone.title = "变更手机号"
        
        navigationController?.pushViewController(modifyPhone, animated: true)
    }
    @objc private func pushBindEmail() {
        
        let modifyEmail = CMDBModifyEmailViewController()
        
        modifyEmail.title = "变更邮箱"
        
        navigationController?.pushViewController(modifyEmail, animated: true)
        
    }
}